/* INSTRUCTION: This is a command line application. So please execute this template with the following arguments:

 arg[0] = username
 arg[1] = password
 */

/**
 * @author (Your Name Here)
 *
 */

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.List;

import com.google.gdata.client.docs.DocsService;
import com.google.gdata.data.MediaContent;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.docs.DocumentEntry;
import com.google.gdata.data.docs.DocumentListEntry;
import com.google.gdata.data.docs.DocumentListFeed;
import com.google.gdata.data.media.MediaSource;
import com.google.gdata.util.ServiceException;

public class Documents {

  public static void main(String[] args) throws IOException, ServiceException {

    System.out.println("uploading file....");
    upload("files\\Fight club.doc", "fightClub");
    System.out.println("file uploaded.");

    System.out.println("downloading file....");
    download("files\\Fight club.html", "fightClub");
    System.out.println("file downloaded.");
    
    /*System.out.println("uploading file....");
    upload("files\\convertedFile.html", "convertedFile");
    System.out.println("file uploaded.");
    */
  

   
  }

  static void upload(String fileLocation, String fileNameOnGDocs) throws IOException, ServiceException {
    DocsService myService = new DocsService("My Application");
    myService.setUserCredentials("lilit.hakobyan14@gmail.com", "102030salut");
    URL metafeedUrl = new URL("https://docs.google.com/feeds/default/private/full");
    File file = new File(fileLocation);
    String mimeType = DocumentListEntry.MediaType.fromFileName(file.getName()).getMimeType();
    DocumentEntry newDocument = new DocumentEntry();
    newDocument.setFile(file, mimeType);
    newDocument.setTitle(new PlainTextConstruct(fileNameOnGDocs));
    myService.insert(metafeedUrl, newDocument);

  }

  static void download(String fileDestination, String fileNameOnGDocs) throws IOException, ServiceException {
    DocsService myService = new DocsService("My Application");
    myService.setUserCredentials("lilit.hakobyan14@gmail.com", "102030salut");

    URL metafeedUrl = new URL("https://docs.google.com/feeds/default/private/full");
    DocumentListFeed resultFeed = myService.getFeed(metafeedUrl, DocumentListFeed.class);
    List<DocumentListEntry> entries = resultFeed.getEntries();
    String docId = "";

    for (int i = 0; i < entries.size(); i++) {
      DocumentListEntry entry = entries.get(i);
      if (entry.getTitle().getPlainText().equals(fileNameOnGDocs)){
        docId = entry.getDocId();
        break;
      }
        
      
    }

    URL url = new URL("https://docs.google.com/feeds/download/documents/Export?docID=" + docId
        + "&exportFormat=html");
    MediaContent mc = new MediaContent();
    mc.setUri(url.toString());
    MediaSource ms = myService.getMedia(mc);
    InputStream inStream = null;
    FileOutputStream outStream = null;
    try {
      inStream = ms.getInputStream();
      outStream = new FileOutputStream(fileDestination);
      int c;
      while ((c = inStream.read()) != -1) {
        outStream.write(c);
      }
    } finally {
      if (inStream != null) {
        inStream.close();
      }
      if (outStream != null) {
        outStream.flush();
        outStream.close();
      }
    }

  }
}
